#include <iostream>
#include <fstream>
#include <unordered_map>
#include <sstream>
using namespace std;

class WordTrans{
public:
    void buildMap(const string & filename){
        ifstream ifs(filename);

        if(!ifs){
            cerr << "open file "<< filename << " failed" << endl;
            return;
        }

        string key;
        string line;
        while (ifs >> key, getline(ifs, line)){ // 在文件读取过程中，文件流指针ifs会向后偏移
            _umap[key] = line.substr(1); // 取子串
            cout << key << " : " << _umap[key] << endl;
        }
    }

    void trans(const string & filename){
        ifstream ifs(filename);

        if(!ifs){
            cerr << "open file "<< filename << " failed" << endl;
            return;
        }

        string line;
        while (getline(ifs, line)){
            istringstream iss(line);
            string word;

            while (iss >> word){
                auto it = _umap.find(word); // find返回一个迭代器
                //其他方法：count结合下标来访问：
                //count = 0 ： 直接输出word
                //count = 1 ： 下标访问umap[word]

                if (it != _umap.end()){
                    cout << _umap[word];
                } else {
                    cout << word;
                }
                cout << " ";
            }
            cout << endl;
        }
    }
private:
    unordered_map<string, string> _umap;
};


int main(){
    unordered_map<string, string> map;
    WordTrans wt;

    wt.buildMap("map.txt");
    cout << endl;
    wt.trans("file.txt");

    return 0;
}